<?php
/**
 * @since v1.0
 * @modify 2015.6.21
 * @copyright 深圳亿游
 */
require_once(LIB_PATH.'controller/root.con.php');

class summaryCon extends rootCon
{
    public function listAction() {
        $uid = mylogin::getUid(true);
        $this->createSummary($uid);


        $obj = new Summary();
        $result = $obj->all(['uid',$uid]);
        $this->j(0, "列表",$result);
    }

    private function createSummary($uid){
        $obj = new Summary();
        $one = $obj->getOne("SELECT * FROM ".$obj->table()." WHERE uid=$uid ORDER BY ".$obj->key()." DESC LIMIT 1");
        $lastMonth = 0;
        if($one){
            $lastMonth = $one['month']."-01 00:00:00";
        }

        $sql = "SELECT status,created FROM tbl_school WHERE created>='".$lastMonth."' AND uid=$uid ORDER BY created";
        $school = new School();
        $all = $school->query($sql);


        $preMonth = '';
        $addCount = 0;//增加
        $checkCount = 0;//待审核
        $passCount = 0;//通过数
        $rejectCount = 0;//拒绝
        foreach($all as $row){
            $time = $row['created'];
            $currentMonth = substr($time,0,7);
            if($currentMonth != $preMonth){//新的月份,保存上一个月数据
                $this->saveSummary($uid,$preMonth,$addCount,$checkCount,$passCount,$rejectCount);

                $addCount = 1;//增加
                $checkCount = 0;//待审核
                $passCount = 0;//通过数
                $rejectCount = 0;//拒绝

                if($row['status']==STATUS_SUBMIT) $checkCount=1;
                if($row['status']==STATUS_PUBLISH) $passCount=1;
                if($row['status']==STATUS_DELETE OR $row['status']==STATUS_ADMIN_DELETE OR $row['status']==STATUS_FORBID) $rejectCount=1;

            }else{//增加统计值
                $addCount++;
                if($row['status']==STATUS_SUBMIT) $checkCount++;
                if($row['status']==STATUS_PUBLISH) $passCount++;
                if($row['status']==STATUS_DELETE OR $row['status']==STATUS_ADMIN_DELETE OR $row['status']==STATUS_FORBID) $rejectCount++;
            }

            $preMonth = $currentMonth;
        }//end foreach

        //保存最后一个月数据
        $this->saveSummary($uid,$preMonth,$addCount,$checkCount,$passCount,$rejectCount);
    }

    private function saveSummary($uid,$preMonth,$addCount,$checkCount,$passCount,$rejectCount){
        if($preMonth && $addCount>0){
            $summary = new Summary();
            $summary->uid = $uid;
            $summary->month = $preMonth;

            $summary->find($summary->getConByLoad());

            $summary->addCount = $addCount;
            $summary->checkCount = $checkCount;
            $summary->passCount = $passCount;
            $summary->rejectCount = $rejectCount;
            $summary->save();
        }
    }
}



